package com.cn.yz.springboot.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TableSplitUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(TableSplitUtil.class);
	
	/***
	 * column转换property
	 * @param field
	 * @return
	 */
	public static String fieldKeyTransField(String field){
		if(field==null){
			return null;
		}
		
		StringBuilder fieldKey = new StringBuilder();
		String[] strs = field.split("_");
		if(strs.length>1){
			for(String str : strs){
				if(fieldKey.length()==0){
					fieldKey.append(str);
				}else {
					fieldKey.append(str.substring(0, 1).toUpperCase()).append(str.substring(1));
				}
			}
		} else {
			fieldKey.append(field);
		}
		return fieldKey.toString();
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> paramMapTransObj(Object obj){
		if(obj==null){
			return null;
		}
		//只支持两层迭代
		if(obj instanceof Map) {
			Map<String, Object> map = (Map) obj;
			Map returnMap = new HashMap();
			for(Map.Entry entry:map.entrySet()){
				if(entry.getValue()!=null&&entry.getValue() instanceof Map){
					returnMap.putAll((Map)entry.getValue());
				}else{
					returnMap.put(entry.getKey(), entry.getValue());
				}
			}
			return returnMap;
		}
		return ObjTransUtils.transBean2Map(obj);
	}
	
	public static void main(String[] args){
		Map obj = new HashMap();
		Map condition = new HashMap();
		String id= "64a105a9-b65b-4e6b-99f6-284f97eed2e5";
		String opYear = "2016";
		condition.put("opYear", opYear);
		condition.put("bussinessId", "2");
		obj.put("id", id);
		obj.put("condition", condition);
		obj.put("param1", id);
		obj.put("param2", condition);
		System.out.println(obj);
		Map ret = TableSplitUtil.paramMapTransObj(obj);
		Iterator it = ret.keySet().iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}
